package com.zhiyou.app.shop.manager.action;

import org.apache.commons.lang3.StringUtils;

import com.zhiyou.app.shop.common.AycshopException;
import com.zhiyou.app.shop.manager.dao.pojo.Goods;
import com.zhiyou.app.shop.manager.dao.pojo.GoodsCategory;
import com.zhiyou.app.shop.manager.dao.pojo.Shop;
import com.zhiyou.app.shop.manager.dao.pojo.UserDetail;
/**
 * 用于简单的表单验证
 * @author zhangkeweigy
 */
public class Validator {
	
	/**验证添加及编辑用户**/
	public static void validateUserAdd(UserDetail ud) {
		if(StringUtils.isBlank(ud.getId())){//如果是添加
			if(StringUtils.isBlank(ud.getLoginName())){
				throw new AycshopException("shopManage.userLoginName.required");
			}
		}
		if(StringUtils.isBlank(ud.getFullName())){
			throw new AycshopException("shopManage.fullName.required");
		}
		if(StringUtils.isBlank(ud.getHandphone())){
			throw new AycshopException("shopManage.handphone.required");
		}
	}
	
	/**验证添加及编辑商品分类**/
	public static void validateGoodsCatAdd(GoodsCategory cat) {
		if(StringUtils.isBlank(cat.getName())){
			throw new AycshopException("shopManage.goodsCategoryName.required");
		}
		if(StringUtils.isBlank(cat.getId())){
			if(null == cat.getGoodsCategoryImg()){
				throw new AycshopException("shopManage.goodsCategoryImg.required");
			}
		}else{
			if(StringUtils.isBlank(cat.getImgPath())){
				throw new AycshopException("shopManage.goodsCategoryImg.required");
			}
		}
	}
	/**验证商铺添加及修改**/
	public static void validateShopAdd(Shop shop) {
		if(StringUtils.isBlank(shop.getName())){
			throw new AycshopException("shopManage.shopName.required");
		}
		if(StringUtils.isBlank(shop.getId())){
			if(null == shop.getShopImg()){
				throw new AycshopException("shopManage.shopImg.required");
			}
		}else {
			if(StringUtils.isBlank(shop.getImgPath())){
				throw new AycshopException("shopManage.shopImg.required");
			}
		}
		if(StringUtils.isBlank(shop.getAddress())){
			throw new AycshopException("shopManage.shopAddress.required");
		}
	}
	public static boolean isBlank(String str){
		return StringUtils.isBlank(str);
	}
	
	/**验证商品添加及修改**/
	public static void validateGoodsAdd(Goods goods) {
		if(isBlank(goods.getName())){
			throw new AycshopException("shopManage.goodsName.required");
		}
		if(null == goods.getPrice()){
			throw new AycshopException("shopManage.goodsPrice.required");
		}
		if(isBlank(goods.getGoodsCategory().getId())){
			throw new AycshopException("shopManage.goodsCategory.required");
		}
		if(isBlank(goods.getGoodsImgPath())){
			throw new AycshopException("shopManage.goodsImg.required");
		}
	}

}
